package com.financial.business.management.service.impl;

import com.financial.business.management.constants.FinancialPayConfigEnumConstants;
import com.financial.business.management.constants.FinancialPlatfromConfigEnumConstants;
import com.financial.business.management.dao.FinancialPaydetailsConfigDao;
import com.financial.business.management.domain.FinancialPayConfigDO;
import com.financial.business.management.domain.FinancialPaydetailsConfigDO;
import com.financial.business.management.domain.FinancialPlatfromConfigDO;
import com.financial.business.management.service.FinancialPayConfigService;
import com.financial.business.management.service.FinancialPaydetailsConfigService;
import com.financial.business.management.service.FinancialPlatfromConfigService;
import com.financial.business.management.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service
public class FinancialPaydetailsConfigServiceImpl implements FinancialPaydetailsConfigService {
	@Autowired
	private FinancialPaydetailsConfigDao financialPaydetailsConfigDao;
    @Autowired
    private FinancialPayConfigService financialPayConfigService;
    @Autowired
    private FinancialPlatfromConfigService financialPlatfromConfigService;

	@Override
	public FinancialPaydetailsConfigDO get(Integer id) {

	    return financialPaydetailsConfigDao.get(id);
	}

	@Override
	public List<FinancialPaydetailsConfigDO> list(Map<String, Object> map) {

	    return financialPaydetailsConfigDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return financialPaydetailsConfigDao.count(map);
	}

	@Override
	public int save(FinancialPaydetailsConfigDO financialPaydetailsConfig) {
		return financialPaydetailsConfigDao.save(financialPaydetailsConfig);
	}

	@Override
	public int update(FinancialPaydetailsConfigDO financialPaydetailsConfig) {
		return financialPaydetailsConfigDao.update(financialPaydetailsConfig);
	}


	@Override
	public int remove(Integer id){
		return financialPaydetailsConfigDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return financialPaydetailsConfigDao.batchRemove(ids);
	}

	@Override
	public ApiResult createPayDetails(Map<String, Object> requestMap) {
	    //获取支付通道类型
        String payType = requestMap.get("payType").toString();
        //获取平台应用标识
        String platfromMarking = requestMap.get("platfromMarking").toString();
        if(StringUtils.isEmpty(payType)||StringUtils.isEmpty(platfromMarking)){
            return ApiResult.paramIsNull();
        }
        FinancialPaydetailsConfigDO paydetailsConfigDO = new FinancialPaydetailsConfigDO();
        //根据支付通道类型查询主id
        Map<String, Object> payMap = new HashMap<>();
        payMap.put("payType",payType);
        payMap.put("isDelete", FinancialPlatfromConfigEnumConstants.ENUM_ISDELETE_UNDEL);
        List<FinancialPayConfigDO> PayConfigList = financialPayConfigService.list(payMap);
        FinancialPayConfigDO financialPayConfigDO = PayConfigList != null && PayConfigList.size() > 0 ? PayConfigList.get(0) : null;
        paydetailsConfigDO.setPayChannelsid(financialPayConfigDO.getId());

        //根据平台应用标识查询主id
        Map<String, Object> platfromConfigMap = new HashMap<>();
        platfromConfigMap.put("platfromMarking",platfromMarking);
        platfromConfigMap.put("isDelete", FinancialPayConfigEnumConstants.ENUM_ISDELETE_UNDEL);
        List<FinancialPlatfromConfigDO> list = financialPlatfromConfigService.list(platfromConfigMap);
        FinancialPlatfromConfigDO financialPlatfromConfigDO = list != null && list.size() > 0 ? list.get(0) : null;
        if (financialPlatfromConfigDO == null){
            return ApiResult.failure();
        }
        Integer ids = financialPlatfromConfigDO.getId();
        paydetailsConfigDO.setPlatfromId(ids);
        paydetailsConfigDO.setGmtCreate(new Date());
        paydetailsConfigDO.setGmtModified(new Date());
        paydetailsConfigDO.setIsDelete(FinancialPayConfigEnumConstants.ENUM_ISDELETE_UNDEL);
        //TODO 参数配置后续需要后台管理系统添加
        paydetailsConfigDO.setParameter("");
        int sum = financialPaydetailsConfigDao.save(paydetailsConfigDO);
        if(sum>0){
            return ApiResult.success();
        }
        //保存支付详情信息
        return ApiResult.failure();
	}

    @Override
    public String getPayBusinessPram(String platfromId ,String channelsId) {
        if(StringUtils.isEmpty(platfromId)|| StringUtils.isEmpty(channelsId)){
            return String.valueOf(ApiResult.paramIsNull());
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("platfrom_id",Integer.valueOf(platfromId));
        resultMap.put("pay_channelsId",Integer.valueOf(channelsId));
        resultMap.put("is_delete",FinancialPlatfromConfigEnumConstants.ENUM_ISDELETE_UNDEL);
        //根据平台应用标识查询主id 平台标识查询平台支付主id获取详细支付信息
        List<FinancialPaydetailsConfigDO> detailslist = financialPaydetailsConfigDao.list(resultMap);
        FinancialPaydetailsConfigDO financialPaydetailsConfigDO = detailslist != null && detailslist.size() > 0 ? detailslist.get(0) : null;
        String parameter = financialPaydetailsConfigDO.getParameter();
        return parameter;
    }

    @Override
    public String getYSBusiness(String platfromId, String channelsId) {

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("platfrom_id",Integer.valueOf(platfromId));
        resultMap.put("pay_channelsId",Integer.valueOf(channelsId));
        resultMap.put("is_delete",FinancialPlatfromConfigEnumConstants.ENUM_ISDELETE_UNDEL);
        //根据平台应用标识查询主id 平台标识查询平台支付主id获取详细支付信息
        List<FinancialPaydetailsConfigDO> detailslist = financialPaydetailsConfigDao.list(resultMap);
        FinancialPaydetailsConfigDO financialPaydetailsConfigDO = detailslist != null && detailslist.size() > 0 ? detailslist.get(0) : null;
        String parameter = financialPaydetailsConfigDO.getParameter();
        System.out.println("据平台应用标识查询主id 平台标识查询平台支付主id获取详细支付信息"+parameter);
        return parameter;
    }

}
